package com.woniuxy.lrz.carlproject.common.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.InputStream;

public class OssTemplate {
    private static String endpoint = "oss-cn-chengdu.aliyuncs.com";
    private static String keyId = "LTAI5tEa5HrLCNqMvCn4C2ns";
    private static String keySecret = "h93QcyYkWDkZDhZbz2CTQNr1IpZBc9";

    public static String fileUpload(String name, InputStream file, String bucketName) {
        OSS oss = new OSSClientBuilder().build("https://" + endpoint, keyId, keySecret);
        String url = null;
        try {
            oss.putObject(bucketName, name, file);
            url = "https://" + bucketName + "." + endpoint + "/" + name;
        } finally {
            if (oss != null) {
                oss.shutdown();
            }
        }
        return url;
    }

    public static boolean deleteFile(String name, String buckeName) {
        OSS oss = new OSSClientBuilder().build("https://" + endpoint, keyId, keySecret);
        String url = null;
        try {
            oss.deleteObject(buckeName, name);
        } finally {
            if (oss != null) {
                oss.shutdown();
            }
        }
        return true;
    }

}
